package com.longrise.android.byjk.plugins.hra.hratabthird;

import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.plugins.hra.TimeUtil;
import com.longrise.common.utils.DZZWTools;
import com.longrise.common.utils.PrintLog;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/1/9.
 */

public class MineEndorseAdapter extends BaseQuickAdapter<EntityBean, BaseViewHolder> {

    public static final String TAG = "MineEndorseAdapter";
    private OnItemClickListener mListener;
    private List<EntityBean> mListBeans = new ArrayList<>();
    private int signNum = 3;
    private long timeBeforeZero;       //24点毫秒数
    private long nowMills;            //当前时间毫秒数
    private long timeEnd = 900000;   //提前15分钟改签


    public MineEndorseAdapter() {
        super(R.layout.item_mine_endorse);
    }

    public void refreshData(EntityBean[] beans) {
        mListBeans.clear();
        for (EntityBean bean : beans) {
            mListBeans.add(bean);
        }
        PrintLog.e(TAG,"mListBeans111=:"+mListBeans);
        setNewData(mListBeans);
    }

    @Override
    protected void convert(BaseViewHolder helper, final EntityBean item) {

        final View convertView = helper.getConvertView();
        final TextView mine_endorse_area = (TextView) convertView.findViewById(R.id.item_mine_endorse_area);
        TextView mine_endorse_name = (TextView) convertView.findViewById(R.id.item_mine_endorse_name);
        TextView mine_endorse_time = (TextView) convertView.findViewById(R.id.item_mine_endorse_time);
        TextView mine_endorse_sign = (TextView) convertView.findViewById(R.id.tv_mine_endorse_sign);
        final LinearLayout ll_mine_endorse_area = (LinearLayout) convertView.findViewById(R.id.ll_mine_endorse_area);

        mine_endorse_area.setText(item.getString("storename"));
        mine_endorse_name.setText(item.getString("username"));
        mine_endorse_time.setText(item.getString("examinedate"));
        final int endorse = Integer.parseInt(item.getString("endorse"));
        String examine = item.getString("examine");

//        //TODO 判断当前时间距离检测时间的差以及可改签次数
        if(0 == signNum - endorse){
            mine_endorse_sign.setVisibility(View.GONE);
        }else{
            mine_endorse_sign.setVisibility(View.VISIBLE);

        }

        //当前时间的毫秒数
        nowMills = TimeUtil.getNow();
//        String todayData = TimeUtil.getTodayData();

        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            if(!TextUtils.isEmpty(examine)){
                date = sdf.parse(examine);
                String dateBeforeZero = TimeUtil.getDateBeforeZero(date, 1);
                PrintLog.e(TAG,"nowMills=:"+nowMills);
                PrintLog.e(TAG,"dateBeforeZero=:"+dateBeforeZero);
                timeBeforeZero = sdf.parse(dateBeforeZero).getTime();
                PrintLog.e(TAG,"timeBeforeZero=:"+timeBeforeZero);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            PrintLog.e(TAG,e.toString());
        }

        mine_endorse_sign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if((timeBeforeZero - nowMills)>= 0){
                    if (mListener != null) {
                        mListener.onClick(item);
                    }
                }else {
                    DZZWTools.showToast(mContext,"请合理安排改签时间",5000);
                }
                if(1 == signNum - endorse){
                    DZZWTools.showToast(mContext,"你的可改签次数仅剩1次",5000);
                }
            }
        });

//        mine_endorse_area.post(new Runnable() {
//            @Override
//            public void run() {
//                int lineCount = mine_endorse_area.getLineCount();
//                ViewGroup.LayoutParams layoutParams1 = ll_mine_endorse_area.getLayoutParams();
//                ViewGroup.LayoutParams layoutParams = convertView.getLayoutParams();
//                if (lineCount >= 2) {
//                    layoutParams1.height = AppUtil.dip2px(70);
//                    layoutParams.height = AppUtil.dip2px(200);
//                } else {
//                    layoutParams1.height = AppUtil.dip2px(47);
//                    layoutParams.height = AppUtil.dip2px(177);
//                }
//                ll_mine_endorse_area.setLayoutParams(layoutParams1);
//                convertView.setLayoutParams(layoutParams);
//            }
//        });

    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    interface OnItemClickListener {
        void onClick(EntityBean item);
    }
}
